#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/10l8ay/9272012_challenge_101_easy_nonrepeating_years/

require 'optparse'
require 'date'

start = "1980"
stop  = "1987"

OptionParser.new do |o|
  o.on("--start=START", String) { |start| }
  o.on("--stop=STOP", String) { |stop| }
end.parse!

raise "Invalid start '#{start}'" unless /^\d{4}$/.match(start)
raise "Invalid stop '#{stop}'" unless /^\d{4}$/.match(stop)

start = Date.strptime(start, "%Y").year
stop  = Date.strptime(stop, "%Y").year

raise "Start '#{start}' should come before stop '#{stop}'" unless start <= stop

(start .. stop).each do |year|
  # General idea:
  #
  # [1] Get the frequency of each digit in the year
  # [2] All years we want to display have 1 for all
  #     of their frequency values. Kick out ones that
  #     do not

  freq = year.to_s.split(//).inject({}) { |r, i| r[i] ||= 0 ; r[i] += 1 ; r }

  puts year unless freq.values.sort.uniq.size > 1
end
